const api = require('../../api/api.js');
const util = require('../../utils/util.js');

Page({
  data: {
    coordinate: {
      lng: new Number(),
      lat: new Number()
    },
    markers: []
  },
  onLoad(o) {
    let that = this;
    api.querySitesByKeyword({
      data: {
        keys: ''
      },
      success(res) {
        console.log(res.data);
        let sites = res.data.sites;
        let markers = sites.map((i) => {
          return {
            id: i.siteId,
            latitude: i.lat,
            longitude: i.lng,
            callout: {
              content: `${i.county}-${i.siteName}`,
              fontSize: 17,
              color: '#0D2F8A',
              borderRadius: 8,
              bgColor: '#fff',
              padding: 10,
              display: 'BYCLICK'
            }
          }
        });
        that.setData({
          markers: markers,
          coordinate: {
            lat: sites[0].lat,
            lng: sites[0].lng
          }
        });
      }
    });
  },
  scan() {
    wx.scanCode({
      success(res) {
        wx.scanCode({
          success(res) {
            console.log(2222, res);
            let data = res.result;
            let url = data.substring(data.lastIndexOf('?'), data.length);
            let json = util.getQueryStringArgs(url);
            console.log(json);
            wx.navigateTo({
              url: `/pages/suyuan-detail/suyuan-detail?id=${json.id}`,
            })
          }
        })
      }
    })
  },
  goToSearchPage() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },
  goToSitePage(e) {
    let siteId = e.markerId;
    wx.navigateTo({
      url: `/pages/site/site?siteId=${siteId}`,
    })
  }
})